package com.invite.me.ui.cehualan;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.adpater.MyPriceAdpater;
import com.invite.me.beans.presenter.MyPricePresenter;
import com.invite.me.beans.res.MyPriceZhifbZrBean;
import com.invite.me.beans.res.MyPricelistBean;
import com.invite.me.ui.pay.PayResult;
import com.invite.me.ui.pay.RolloutPreiceActivity;
import com.invite.me.util.ToastUtils;
import com.invite.me.wlqq.Contract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyPriceActivity extends BaseActivity implements View.OnClickListener,Contract.BaseView {
    MyPricePresenter myPricePresenter;

    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Log.e("dingdan", "该笔订单是否真实支付成功，需要依赖服务端的异步通知。" );


                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Log.e("dingdan", "该笔订单真实的支付结果，需要依赖服务端的异步通知。" );
                        ToastUtils.show(MyPriceActivity.this,"支付取消");
                    }
                    break;
                }
//                case SDK_AUTH_FLAG: {
//                    @SuppressWarnings("unchecked")
//                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
//                    String resultStatus = authResult.getResultStatus();
//
//                    // 判断resultStatus 为“9000”且result_code
//                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
//                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
//                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
//                        // 传入，则支付账户为该授权账户
//                        showAlert(PayDemoActivity.this, getString(R.string.auth_success) + authResult);
//                    } else {
//                        // 其他状态值则为授权失败
//                        showAlert(PayDemoActivity.this, getString(R.string.auth_failed) + authResult);
//                    }
//                    break;
//                }
                default:
                    break;
            }
        };
    };

    private SmartRefreshLayout myxiangqinsmartrefresh;
    MyPriceAdpater myPriceAdpater;
    List<MyPricelistBean> list=new ArrayList<>();


    @Override
    protected void initView() {
        myPricePresenter=new MyPricePresenter(this);
        //    initdata();
        ImageView imagefanhui=findViewById(R.id.imagefanhui);
        imagefanhui.setOnClickListener(this);
        TextView zhuanchu=findViewById(R.id.zhuanchu);
        TextView zhuanru=findViewById(R.id.zhuanru);
        myxiangqinsmartrefresh = findViewById(R.id.myxiangqinsmartrefresh);
        myxiangqinsmartrefresh.setEnableRefresh(false);//是否启用下拉刷新功能
        myxiangqinsmartrefresh.setEnableLoadMore(true);//是否启用上拉加载功能
        RecyclerView qianrecyclerview=findViewById(R.id.qianrecyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        qianrecyclerview.setLayoutManager(layoutManager);
        myPriceAdpater=new MyPriceAdpater(R.layout.mypricelist,list);
        qianrecyclerview.setAdapter(myPriceAdpater);
        //   myPriceAdpater.notifyDataSetChanged();

        zhuanchu.setOnClickListener(this);
        zhuanru.setOnClickListener(this);

    }
    public void initdata(){
        for (int i=0;i<1;i++){
            list.add(new MyPricelistBean("娃娃"));
            list.add(new MyPricelistBean("娃娃"));
            list.add(new MyPricelistBean("娃娃"));
            list.add(new MyPricelistBean("娃娃"));
            list.add(new MyPricelistBean("娃娃"));
            list.add(new MyPricelistBean("娃娃"));

        }


    }
    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_myprice;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zhuanchu:
                Intent zhuchu = new Intent(MyPriceActivity.this, RolloutPreiceActivity.class);
                startActivity(zhuchu);
                break;
            case R.id.zhuanru:
                myPricePresenter.zhifubaozhifu("1232342","3223234","20");



                break;
            case R.id.imagefanhui:
                finish();
                break;

        }

    }

    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (myPricePresenter instanceof Contract.BasePresenter) {
            this.myPricePresenter = (MyPricePresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o){
        if(o instanceof MyPriceZhifbZrBean){
            MyPriceZhifbZrBean myPriceZhifbZrBean= (MyPriceZhifbZrBean) o;
            final String data = myPriceZhifbZrBean.getData();
            Runnable payRunnable = new Runnable() {

                @Override
                public void run() {
                    PayTask alipay = new PayTask(MyPriceActivity.this);
                    Map<String,String> result = alipay.payV2(data,true);

                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = result;
                    mHandler.sendMessage(msg);
                }
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();




        }

    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
}